﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace SyncConfig.Dialog
{
    public partial class AddClient : SyncConfig.Dialog.BaseDialog
    {
        public AddClient()
        {
            InitializeComponent();
            Text = "Thêm mới máy trạm";
        }
        
        DataRowView _row;
        public void DataBind(DataRowView row)
        {
            _row = row;           
            txtClientName.Text = row["ClientName"].ToString();
            txtDes.Text = row["Des"].ToString();
            Text = "Thông tin máy trạm";
        }
        public void New()
        {
            _row = null;            
            txtClientName.Text = "";
            txtDes.Text = "";
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtClientName.Text.Trim()))
            {
                MessageBox.Show("Chưa nhập tên máy trạm");
                return;
            }
           
            
            try
            {
                if (_row == null)
                {                    
                    _row = FrmMain.MainForm.TreeManipulation.dsSyncConfig.Client.DefaultView.AddNew();
                }
                _row.BeginEdit();
                if (_row.Row.IsNull("ClientId"))
                    _row["ClientId"] = Guid.NewGuid();
                _row["ClientName"] = txtClientName.Text;
                _row["Des"] = txtDes.Text;
                _row.EndEdit();
                FrmMain.MainForm.TreeManipulation.AdapterClientTable.Update(_row.Row);
                FrmMain.MainForm.TreeManipulation.dsSyncConfig.Client.AcceptChanges();
                DialogResult = DialogResult.OK;
            }
            catch (Exception ex)
            {
                FrmMain.MainForm.TreeManipulation.dsSyncConfig.Client.RejectChanges();
                MessageBox.Show(ex.Message);
            }
            

                
        }
    }
}
